package com.ruoyi.hotel.service.impl;

import java.util.List;
import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hotel.domain.vo.ChangeHouseStatus;
import com.ruoyi.hotel.domain.vo.HotelStatusNumber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hotel.mapper.HotelHouseMapper;
import com.ruoyi.hotel.domain.HotelHouse;
import com.ruoyi.hotel.service.IHotelHouseService;

/**
 * 房间Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-15
 */
@Service
public class HotelHouseServiceImpl extends ServiceImpl<HotelHouseMapper, HotelHouse> implements IHotelHouseService
{
    @Autowired
    private HotelHouseMapper hotelHouseMapper;

    /**
     * 查询房间
     *
     * @param houseId 房间主键
     * @return 房间
     */
    @Override
    public HotelHouse selectHotelHouseByHouseId(Long houseId)
    {
        return hotelHouseMapper.selectHotelHouseByHouseId(houseId);
    }

    /**
     * 查询房间列表
     *
     * @param hotelHouse 房间
     * @return 房间
     */
    @Override
    public List<HotelHouse> selectHotelHouseList(HotelHouse hotelHouse)
    {
        return hotelHouseMapper.selectHotelHouseList(hotelHouse);
    }

    /**
     * 新增房间
     *
     * @param hotelHouse 房间
     * @return 结果
     */
    @Override
    public int insertHotelHouse(HotelHouse hotelHouse)
    {
    Date date = DateUtils.getNowDate();
            hotelHouse.setCreateTime(date);
            hotelHouse.setUpdateTime(date);
        return hotelHouseMapper.insert(hotelHouse);
    }

    /**
     * 修改房间
     *
     * @param hotelHouse 房间
     * @return 结果
     */
    @Override
    public int updateHotelHouse(HotelHouse hotelHouse)
    {
        hotelHouse.setUpdateTime(DateUtils.getNowDate());
        return hotelHouseMapper.updateById(hotelHouse);
    }

    /**
     * 批量删除房间
     *
     * @param houseIds 需要删除的房间主键
     * @return 结果
     */
    @Override
    public int deleteHotelHouseByHouseIds(Long[] houseIds)
    {
        return hotelHouseMapper.deleteHotelHouseByHouseIds(houseIds);
    }

    /**
     * 删除房间信息
     *
     * @param houseId 房间主键
     * @return 结果
     */
    @Override
    public int deleteHotelHouseByHouseId(Long houseId)
    {
        return hotelHouseMapper.deleteHotelHouseByHouseId(houseId);
    }

    @Override
    public boolean checkHouseNumUnique(HotelHouse hotelHouse) {
        Long houseId = StringUtils.isNull(hotelHouse.getHouseId()) ? -1L : hotelHouse.getHouseId();
        HotelHouse info = hotelHouseMapper.checkHouseNumUnique(hotelHouse.getHouseNum());
        if (StringUtils.isNotNull(info) && info.getHouseId().longValue() != houseId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public boolean changeHouseStatus(ChangeHouseStatus changeHouseStatus) {
        Integer houseStatus = changeHouseStatus.getHouseStatus();
        if(houseStatus==3){
            throw new ServiceException("不能更改房间状态为已入住");
        }
        HotelHouse hotelHouse = this.getById(changeHouseStatus.getHouseId());
        if(hotelHouse ==null){
            throw new ServiceException("请选择要更改状态的房间");
        }
        if(hotelHouse.getHouseStatus()==3){
            throw new ServiceException("当前房间已入住状态，不能更改");
        }
        LambdaUpdateWrapper<HotelHouse> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(HotelHouse::getHouseStatus,changeHouseStatus.getHouseStatus());
        updateWrapper.eq(HotelHouse::getHouseId,changeHouseStatus.getHouseId());
        return this.update(updateWrapper);
    }

    @Override
    public List<HotelStatusNumber> getHouseStatusNumber() {
        return hotelHouseMapper.getHouseStatusNumber();
    }
}
